package com.liuchuanzheng.lcz_animationdemos;

import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import android.widget.ThemedSpinnerAdapter;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;

import com.liuchuanzheng.lcz_animationdemos.databinding.ActivityAppStartBinding;
import com.liuchuanzheng.lcz_animationdemos.fragments.TraditionFragment;
import com.liuchuanzheng.lcz_animationdemos.utils.AppUtils;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 刘传政
 * @date 2021/6/18 13:53
 * QQ:1052374416
 * 电话:18501231486
 * 作用:
 * 注意事项:
 */
public class AppStartActivity extends AppCompatActivity {
    private ActivityAppStartBinding mViewBinding;
    private Context mContext;

    @Override
    protected void onCreate(@Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = this;
        mViewBinding = ActivityAppStartBinding.inflate(getLayoutInflater());
        setContentView(mViewBinding.getRoot());
        setSupportActionBar(mViewBinding.toolbar);
        //设置不显示activity的名字.否则toolbar会自动显示,很难看
        getSupportActionBar().setDisplayShowTitleEnabled(false);
        ArrayList<Fragment> fragments = new ArrayList<>();
        TraditionFragment traditionFragment = new TraditionFragment();
        fragments.add(traditionFragment);

        mViewBinding.spinner.setAdapter(new MyAdapter(mContext, getResources().getStringArray(R.array.fragments)));
        mViewBinding.spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                FragmentTransaction transition = getSupportFragmentManager().beginTransaction();
                Fragment fragment = fragments.get(position);
                transition.replace(R.id.container, fragment).commit();
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
        mViewBinding.spinner.setSelection(0);
        setupAutoCompleteTextView();
    }

    // <editor-fold defaultstate="collapsed" desc="设置自动搜索过滤activity控件">
    private void setupAutoCompleteTextView() {
        //截取之后的名字
        List<String> activites = new ArrayList<>();
        //直接获取出来的名字
        List<String> activitesAll = new ArrayList<>();
        PackageManager packageManager = getPackageManager();
        try {
            PackageInfo packageInfo = packageManager.getPackageInfo(AppUtils.getPackageName(this),
                    PackageManager.GET_ACTIVITIES);
            ActivityInfo[] activityInfos = packageInfo.activities;
            for (ActivityInfo activityInfo : activityInfos) {
                String activity = activityInfo.name;
                int dotIndex = activity.lastIndexOf(".");
                String act = activity.substring(dotIndex + 1);
//                Log.e(TAG, "setupAutoCompleteTextView: " + act);
//                Log.e(TAG, "setupAutoCompleteTextView: " + activity);
                if (act.equals("AppStartActivity")) {
                    continue;
                }
                activites.add(act);
                activitesAll.add(activity);
            }
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }

        ArrayAdapter<?> adapter = new ArrayAdapter<>(this,
                android.R.layout.simple_spinner_dropdown_item, activites);
        mViewBinding.acact.setAdapter(adapter);
        mViewBinding.acact.setDropDownBackgroundResource(R.color.white);
        mViewBinding.acact.setOnItemClickListener((parent, view, position, id) -> {
                    try {
                        String content = mViewBinding.acact.getText().toString().trim();
                        if (activites.contains(content)) {
                            int index = activites.indexOf(content);
                            String target = activitesAll.get(index);
                            Class<?> targetClass = Class.forName(target);
                            Intent intent = new Intent(this, targetClass);
                            startActivity(intent);
                            mViewBinding.acact.setText("");
                        }

                    } catch (ClassNotFoundException e) {
                        e.printStackTrace();
                    }
                }
        );
    }
    // </editor-fold>

    class MyAdapter extends ArrayAdapter<String> implements ThemedSpinnerAdapter {
        androidx.appcompat.widget.ThemedSpinnerAdapter.Helper mHelper;

        private MyAdapter(Context context, String[] objects) {
            super(context, android.R.layout.simple_list_item_1, objects);
            mHelper = new androidx.appcompat.widget.ThemedSpinnerAdapter.Helper(context);
        }

        @Override
        public View getDropDownView(int position, @Nullable @org.jetbrains.annotations.Nullable View convertView, @NonNull @NotNull ViewGroup parent) {
            View view;
            if (convertView == null) {
                LayoutInflater inflater = mHelper.getDropDownViewInflater();
                view = inflater.inflate(android.R.layout.simple_list_item_1, parent, false);
            } else {
                view = convertView;
            }
            TextView text1 = view.findViewById(android.R.id.text1);
            text1.setText(getItem(position));
            return view;
        }
    }
}
